`
https://leetcode.cn/problems/count-substrings-with-k-frequency-characters-i/
`

/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var numberOfSubstrings = function (s, k) {
  const n = s.length
  const { get, set } = counter()
  let res = 0
  let left = 0, right = 0

  while (right < n) {
    const c = s[right++]
    set(c, get(c) + 1)

    // 关注 left - 1 的合法性
    while (get(c) === k) {
      const d = s[left++]
      set(d, get(d) - 1)
    }

    // 一共有 left 个
    res += left
  }

  return res
};

function counter() {
  const cnt = new Array(26).fill(0)
  const ordA = 'a'.charCodeAt(0)
  return {
    get(c) {
      return cnt[c.charCodeAt(c) - ordA]
    },
    set(c, val) {
      cnt[c.charCodeAt(c) - ordA] = val
    }
  }
}